package com.jastar.android.game.richman.logic;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;

import com.jastar.android.game.richman.logic.bean.Cell;
import com.jastar.android.game.richman.logic.bean.Player;
import com.jastar.android.game.richman.logic.bean.Position;
import com.jastar.android.game.richman.logic.enums.CellType;
import com.jastar.android.util.Utils;

import java.util.stream.IntStream;

public class GameDrawing extends Game {

    public GameDrawing(Resources resources, int playerCount, int money) {
        super(resources, playerCount, money);
    }

    public void draw(Canvas canvas) {

        drawBackground(canvas); drawCells(canvas); drawDice(canvas); drawPlayers(canvas);

    }

    private void drawBackground(Canvas canvas) {
        canvas.drawColor(Color.WHITE); paint.setColor(0xFFEAEAEF);
        canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
    }

    private void drawCells(Canvas canvas) {
        int width = canvas.getWidth(); int height = canvas.getHeight(); int cellSize_w = width / (lineAverage + 1);
        int cellSize_h = height / (lineAverage + 1); IntStream.range(0, cells.size()).forEach(i -> {
            Cell cell = cells.get(i);

            Position position = getPositionByIndex(canvas, i); int left = position.getX(), top = position.getY();

            drawRectWithLine(canvas, cell.getColor(), left, top, cellSize_w, cellSize_h);

            //分组标识
            if (CellType.LAND == cell.getType()) {
                int color = cell.getGroup().getColor(); int n = i / lineAverage;
                int w = cellSize_w / 5, h = cellSize_h / 5; switch (n) {
                    case 0: //上
                        drawRectWithLine(canvas, color, left, top + cellSize_h - h, cellSize_w, h); break;
                    case 1: //右
                        drawRectWithLine(canvas, color, left, top, w, cellSize_h); break;
                    case 2: //下
                        drawRectWithLine(canvas, color, left, top, cellSize_w, h); break;
                    case 3: //左
                    default:
                        drawRectWithLine(canvas, color, left + cellSize_w - w, top, w, cellSize_h);
                }

            }

            //建筑
            if (CellType.LAND == cell.getType() && null != cell.getLandType() && null != cell.getOwner()) {
                Drawable drawable = levelHouseIcons.get(cell.getLandType().name() + cell.getLevel());
                if (null != drawable) {
                    float ratio = 0.2F + 0.05F * cell.getLevel();//级别越高比例越大
                    drawable.setBounds(Utils.scale(new Rect(left, top, left + cellSize_w, top + cellSize_h), ratio));
                    drawable.draw(canvas);
                }
            }

        });
    }

    private void drawDice(Canvas canvas) {
        if (diceResult <= 0) {
            return;
        } Bitmap tBitmap = diceBitmaps[diceResult - 1]; int w = tBitmap.getWidth(); int h = tBitmap.getHeight();
        canvas.drawBitmap(tBitmap, canvas.getWidth() / 2F - w / 2F, canvas.getHeight() / 2F - h / 2F, null);
    }

    private void drawPlayers(Canvas canvas) {
        if (players != null) {
            for (Player p : players) {

                if (null == p.getBitmap()) {
                    p.loadBitmap(resources, canvas.getHeight() / (lineAverage + 1));
                }

                Bitmap tBitmap = p.getThumbnailBitmap();
                Position position = getPositionByIndex(canvas, p.getPosition());
                int cellSize_w = canvas.getWidth() / (lineAverage + 1);
                int xOffset = (cellSize_w - tBitmap.getWidth()) / 2;
                canvas.drawBitmap(tBitmap, position.getX() + xOffset, position.getY(), null);
            }
        }
    }

    private void drawRectWithLine(Canvas canvas, int color, int l, int t, int w, int h) {
        paint.setStyle(Paint.Style.FILL); paint.setColor(color); canvas.drawRect(l, t, l + w, t + h, paint);
        paint.setStyle(Paint.Style.STROKE); paint.setColor(LINE_COLOR); canvas.drawRect(l, t, l + w, t + h, paint);
    }

    private Position getPositionByIndex(Canvas canvas, int index) {
        int cellSize_w = canvas.getWidth() / (lineAverage + 1); int cellSize_h = canvas.getHeight() / (lineAverage + 1);
        int x, y; if (index <= lineAverage) {
            x = index * cellSize_w; y = 0;
        } else if (index <= lineAverage * 2) {
            x = lineAverage * cellSize_w; y = (index - lineAverage) * cellSize_h;
        } else if (index <= lineAverage * 3) {
            x = (lineAverage * 3 - index) * cellSize_w; y = lineAverage * cellSize_h;
        } else if (index <= lineAverage * 4) {
            x = 0; y = (lineAverage * 4 - index) * cellSize_h;
        } else {
            throw new RuntimeException("getPositionByIndex : wrong index.");
        } return new Position(x, y);
    }

}
